﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Cabal.Kernel.Provider
{
    /// <summary>
    /// CabalSoul环境支持系统,提供对XML的安全访问
    /// </summary>
    public class EnvSupport:IDisposable
    {
        public EnvSupport()
        {
            _docs = new Dictionary<string, XmlDocument>();
        }


        Dictionary<string, XmlDocument> _docs;
        public Dictionary<string, XmlDocument> Documents
        {
            get
            {
                return _docs;
            }
        }
        public bool RegisterOverwrite = true;


        public void Register(string regid, string path)
        {
            if (RegisterOverwrite == true && _docs.ContainsKey(regid))
                _docs.Remove(regid);

            XmlDocument doc = new XmlDocument();
            doc.Load(path);
            _docs.Add(regid, doc);
        }
        public void RegisterXML(string regid, string xml)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            _docs.Add(regid, doc);
        }
        public void UnRegister(string regid)
        {
            _docs.Remove(regid);
        }
        public void Reload(string regid, string path)
        {
            if (_docs.ContainsKey(regid))
                UnRegister(regid);

            Register(regid, path);
        }

        public string ReadString(string regid, string xpath)
        {
            XmlElement element = (XmlElement)_docs[regid].SelectSingleNode(xpath);
            return element.InnerText;
        }
        public int ReadInt(string regid, string xpath)
        {
            XmlElement element = (XmlElement)_docs[regid].SelectSingleNode(xpath);
            return int.Parse(element.InnerText);
        }
        public XmlNode ReadNode(string regid, string xpath)
        {
            return _docs[regid].SelectSingleNode(xpath);
        }

        public void WriteString(string regid, string xpath, string name, string value, bool overwrite)
        {
            XmlElement element;
            element = _docs[regid].CreateElement(name);
            element.InnerText = value;

            WriteNode(regid, xpath, element, overwrite);
        }
        public void WriteInt(string regid, string xpath, string name, int value, bool overwrite)
        {
            WriteString(regid, xpath, name, value.ToString(), overwrite);
        }
        public void WriteNode(string regid, string xpath, XmlNode node, bool overwrite)
        {
            XmlNode parent;
            XmlDocument doc = _docs[regid];

            parent = doc.SelectSingleNode(xpath);

            if (overwrite)
            {
                XmlNode nodeold = null;

                try { nodeold = node.SelectSingleNode(node.Name); }
                catch { }

                if (nodeold != null)
                    parent.ReplaceChild(node, nodeold);
                else
                    parent.AppendChild(node);
            }
            else
            {
                parent.AppendChild(node);
            }
        }

        public void Save(string regid, string path)
        {
            _docs[regid].Save(path);
        }

        public XmlElement CreatElement(string regid, string name)
        {
            return _docs[regid].CreateElement(name);
        }


        public static void addElement(XmlDocument doc, XmlNode node, string key, string value)
        {
            XmlElement element = doc.CreateElement(key);
            element.InnerText = value;
            node.AppendChild(element);
        }



        #region IDisposable 成员

        public void Dispose()
        {
            _docs.Clear();
            GC.Collect();
        }

        #endregion
    }
}
